#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/1/15 18:00
# @Author : 闫峰
# @File    : view.py
import json
import random
from urllib import parse
from flask import request, Response
from flask_service.service.area_ap_manager.v1_apis import AreaApManagerApis
from util.otherutils.functions_control import get_uuid
from util.handler_request_log import handler_request_log
from util.mongodb_utils import MongoDBLink
from util.redis_control.redis_operations import RedisOperator
user = parse.quote_plus("viviu")
passwd = parse.quote_plus("viu@1234")
modb_client = MongoDBLink(
    uri=f'mongodb://{user}:{passwd}@10.86.3.113:27017/?authSource=admin',
    dbname='sit_charge_mock_data', coll_name='sit_charge_mock_data')
redis_client = RedisOperator()


def v1_aapmgr(path):
    is_forward = True
    path = "/v1/aapmgr/" + path
    request_method = request.method
    # 使用示例
    redis_value = redis_client.get_key("/v1/aapmgr")
    if redis_value:
        if redis_value.upper() == request_method:
            is_forward = False
    sit_base_url = "http://cg-sit-auto-zone.yuntiancloud.com/area-ap-manager" + path
    try:
        request_json = request.json
    except:
        request_json = None
    try:
        request_args = dict(request.args)
    except:
        request_args = None

    resp_json = None
    response = None
    if is_forward:
        status_code, resp_json = AreaApManagerApis.v1_aapmgr_handler(request_method, sit_base_url, request_json,
                                                                     request_args)
        response = Response(json.dumps(resp_json), status=200, mimetype='application/json')
    else:
        status_code = random.choice([500])
        match status_code:
            case 500:
                resp_json = {
                    "ret": {
                        "code": status_code,
                        "reason": f"{path}调用失败",
                        "msg": f"{path}调用失败",
                        "request_id": str(get_uuid()),
                    }
                }
                resp_str = json.dumps(resp_json)
                response = Response(resp_str, status=status_code, mimetype='application/json')
    handler_request_log(request, status_code, resp_json, modb_client, bool(request_json))
    return response


def v1_status(path):
    is_forward = True
    path = "/v1/status/" + path
    # 使用示例
    value = redis_client.get_key(path)
    if value == "1":
        is_forward = False
    sit_base_url = "http://cg-sit-auto-zone.yuntiancloud.com/area-ap-manager" + path
    try:
        request_json = request.json
    except:
        request_json = None
    try:
        request_args = dict(request.args)
    except:
        request_args = None
    request_method = request.method
    resp_json = None
    response = None
    if is_forward:
        status_code, resp_json = AreaApManagerApis.v1_status_handler(request_method, sit_base_url, request_json,
                                                                     request_args)
        response = Response(json.dumps(resp_json), status=200, mimetype='application/json')
    else:
        status_code = random.choice([500])
        match status_code:
            case 500:
                resp_json = {
                    "ret": {
                        "code": status_code,
                        "reason": f"{path}调用失败",
                        "msg": f"{path}调用失败",
                        "request_id": str(get_uuid()),
                    }
                }
                resp_str = json.dumps(resp_json)
                response = Response(resp_str, status=status_code, mimetype='application/json')
    handler_request_log(request, status_code, resp_json, modb_client, bool(request_json))
    return response


def v1_config(path):
    is_forward = True
    path = "/v1/config/" + path
    # 使用示例
    value = redis_client.get_key(path)
    if value == "1":
        is_forward = False
    sit_base_url = "http://cg-sit-auto-zone.yuntiancloud.com/area-ap-manager" + path
    try:
        request_json = request.json
    except:
        request_json = None
    try:
        request_args = dict(request.args)
    except:
        request_args = None
    request_method = request.method
    resp_json = None
    response = None
    if is_forward:
        status_code, resp_json = AreaApManagerApis.v1_config_handler(request_method, sit_base_url, request_json,
                                                                     request_args)
        response = Response(json.dumps(resp_json), status=200, mimetype='application/json')
    else:
        status_code = random.choice([500])
        match status_code:
            case 500:
                resp_json = {
                    "ret": {
                        "code": status_code,
                        "reason": f"{path}调用失败",
                        "msg": f"{path}调用失败",
                        "request_id": str(get_uuid()),
                    }
                }
                resp_str = json.dumps(resp_json)
                response = Response(resp_str, status=status_code, mimetype='application/json')
    handler_request_log(request, status_code, resp_json, modb_client, bool(request_json))
    return response


def v1_db(path):
    is_forward = True
    path = "/v1/db/" + path
    # 使用示例
    value = redis_client.get_key(path)
    if value == "1":
        is_forward = False
    sit_base_url = "http://cg-sit-auto-zone.yuntiancloud.com/area-ap-manager" + path
    try:
        request_json = request.json
    except:
        request_json = None
    try:
        request_args = dict(request.args)
    except:
        request_args = None
    request_method = request.method
    resp_json = None
    response = None
    if is_forward:
        status_code, resp_json = AreaApManagerApis.v1_db_handler(request_method, sit_base_url, request_json,
                                                                 request_args)
        response = Response(json.dumps(resp_json), status=200, mimetype='application/json')
    else:
        status_code = random.choice([500])
        match status_code:
            case 500:
                resp_json = {
                    "ret": {
                        "code": status_code,
                        "reason": f"{path}调用失败",
                        "msg": f"{path}调用失败",
                        "request_id": str(get_uuid()),
                    }
                }
                resp_str = json.dumps(resp_json)
                response = Response(resp_str, status=status_code, mimetype='application/json')
    handler_request_log(request, status_code, resp_json, modb_client, bool(request_json))
    return response
